#include <iostream>
#include "sock.hpp"

void Usage(std::string pro){
  std::cout<< "Usage:" <<pro << "\tport" << std::endl;
}
int main(int argc, char* argv[]){
  if(argc != 2){
    Usage(argv[0]);
    return -1;
  }
  uint16_t port = atoi(argv[1]);
  int sock = sock::Sock_for_Udp();
  sock::Bind(sock, port);
  while(1){
    struct sockaddr_in peer;
    socklen_t len = sizeof(peer);
    char buffer[1024];
    ssize_t s = recvfrom(sock, buffer, sizeof(buffer) - 1, 0, (struct sockaddr*)&peer, &len);
    if(s > 0){
      buffer[s] = 0;
      std::string echo_hello;
      echo_hello += buffer;
      std::cout << "client#" << buffer << std::endl;

      sendto(sock, echo_hello.c_str(), echo_hello.size(), 0, (struct sockaddr*)&peer, len);
    }
    else{
      std::cerr << "recvfrom error" <<std::endl;
    }
  }

  return 0;
}
